package main

import (
	"elegantGo/chapter04/oo"
	"elegantGo/chapter04/pkg/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"net/http"
)

func main() {

	validate.RegisterValidation("int", func(fl validator.FieldLevel) bool {
		var number oo.Number
		return number.IsInt(fl.Field().String())
	})

	validate.RegisterValidation("substringints", func(fl validator.FieldLevel) bool {
		var substring oo.Substring
		substring.SetValue(fl.Field().String())
		return substring.IsIntsValid()
	})

	srv := gin.Default()

	srv.GET("/users", func(c *gin.Context) {
		type Users struct {
			Substring string `binding:"substringints"`
		}

		req := new(Users)
		if err := c.ShouldBind(req); err != nil {
			c.JSON(http.StatusOK, err.Error())
			return
		}

		var substring oo.Substring
		substring.SetValue(req.Substring)

		c.JSON(http.StatusOK, gin.H{
			"Ints": substring.Ints(),
		})
	})

	srv.Run(":80")
}
